package com.cxyd.modules.wms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.Result;
import com.cxyd.common.validator.AssertUtils;
import com.cxyd.common.validator.ValidatorUtils;
import com.cxyd.common.validator.group.AddGroup;
import com.cxyd.common.validator.group.DefaultGroup;
import com.cxyd.common.validator.group.UpdateGroup;
import com.cxyd.modules.wms.dto.StockDTO;
import com.cxyd.modules.wms.dto.ZoneDTO;
import com.cxyd.modules.wms.entity.ZoneEntity;
import com.cxyd.modules.wms.service.ZoneService;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("wms/zone")
public class ZoneController {

    private final ZoneService zoneService;

    public ZoneController(ZoneService zoneService) {
        this.zoneService = zoneService;
    }
    
    @GetMapping("/page")
    @RequiresPermissions("wms:zone:page")
    public Result<PageData<ZoneDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<ZoneDTO> page = zoneService.page(params);

        return new Result<PageData<ZoneDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:zone:info")
    public Result<ZoneDTO> get(@PathVariable("id") Long id) {
        ZoneDTO data = zoneService.get(id);

        return new Result<ZoneDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:zone:save")
    public Result save(@RequestBody ZoneDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        zoneService.save(dto);

        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:zone:update")
    public Result update(@RequestBody ZoneDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        zoneService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("wms:zone:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        zoneService.delete(ids);

        return new Result();
    }
}